package com.rtos.vo.file.popFileContent;

import com.rtos.model.fileSystem.FileUtil;
import com.rtos.vo.file.diskPane.DiskViewer;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import lombok.SneakyThrows;

public class PopFileContent extends Stage {
    TextArea textArea = new TextArea();
    AnchorPane pane;
    Scene scene;
    FileUtil fileUtil;
    String path;
    public DiskViewer diskViewer;
    public PopFileContent(String content, FileUtil fileUtil, String path,DiskViewer diskViewer) {
        this.path =path;
        this.fileUtil=fileUtil;
        this.textArea.setText(content);
        this.textArea.setMinWidth(500);
        this.textArea.setMinHeight(500);
        this.diskViewer=diskViewer;
//        this.textArea.settop
        System.out.println(this.textArea.getText());
        pane = new AnchorPane();
        MenuBar menuBar = new MenuBar();
        Menu menu1 = new Menu("文件");
        MenuItem item1 = new MenuItem("保存");
        menuBar.getMenus().addAll(menu1);
        menu1.getItems().addAll(item1);
//        menuBar.setPrefHeight(50);
        menuBar.setLayoutX(0);
        menuBar.setLayoutY(0);
        textArea.setLayoutY(30);
        menuBar.setPrefWidth(500);

        pane.getChildren().addAll(this.textArea, menuBar);
        scene = new Scene(pane, 500, 500);
        Stage stg = new Stage();
        stg.setScene(scene);
        stg.show();
        item1.setOnAction(new EventHandler<ActionEvent>() {
            @SneakyThrows
            @Override
            public void handle(ActionEvent event) {
//                System.out.println();
                System.out.println("编辑内容是:"+textArea.getText());
                edit(textArea.getText()) ;
                stg.close();

            }
        });
    }
    public void  edit(String s) throws Exception {

        if(fileUtil.modifyFile(path,s)){
            System.out.println("修改成功");
            diskViewer.updateStatus();
//            refresh();
        }
    }


}
